--
-- Author: yawei.zhu
-- Date: 2018-08-03 10:57:44
--
-- RoadHeroRender
--
local NewRareInfoView = require("app.newBag.views.NewRareInfoView")

local ClassRef = app.mvc.createRenderClass(...)

local NODE_NUM = 3

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RoadHeroItemNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui.Panel:getContentSize())
    self._ui.tlt:setString( L("task_road_desc1") )
    self._timeLine = self:getTimeline()

    self._ui["Panel_icon/icon"]:setTouchEnabled(true)
    self._ui["Panel_icon/icon"]:addClickEventListener(function()
        executeFunc(self._onClick, "openHeroBox", self._data)
    end)

    self._ui["Panel_icon/icon_2"]:setTouchEnabled(true)
    self._ui["Panel_icon/icon_2"]:addClickEventListener(function()
        executeFunc(self._onClick, "openHeroBox", self._data)
    end)

    self._ui["Panel_icon/icon_open"]:setTouchEnabled(true)
    self._ui["Panel_icon/icon_open"]:addClickEventListener(function()
        -- self:showRareInfo()
    end)

    for i = 1, NODE_NUM do
        local panel = self._ui["head_"..i]:getChildByName("Panel")
        panel:addClickEventListener(function()
            executeFunc(self._onClick, "openHeroNode", {self._data, i})
        end)
    end

    self:refresh()
end

function ClassRef:refresh()
    if not self._data then return end
    local level = self._data

    self._ui.num:setString( level )

    self._confs = {}
    for i = 1, NODE_NUM do
        local config = GD:getHeroInfoByIndex(level, i, UD:getClass())
        local head = self._ui["head_"..i]
        head:findChild("Panel/num"):setString(level .. "-" .. i)
        head:findChild("Panel/icon"):loadTexture(Res.npcIconPath .. config.icon .. ".png")
        table.insert(self._confs, config)
    end
end

-- 更新英雄之路信息
function ClassRef:updateHeroInfo(process)
    local level = self._data

    for i = 1, NODE_NUM do
        local img = self._ui["line" .. i]:getChildByName("img")
        img:setVisible(false)

        local head = self._ui["head_"..i]
        local panel = head:getChildByName("Panel")

        panel:getChildByName("bg_p"):setVisible(false)
        panel:getChildByName("over"):setVisible(false)
        panel:getChildByName("icon_1"):setVisible(false)
        panel:getChildByName("icon"):setOpacity(255)

        local layer = level
        local order = i
        -- dump(process)

        if layer < process.level or (layer == process.level and order <= process.step) then -- 已经打过
            panel:getChildByName("over"):setVisible(true)
            panel:getChildByName("icon_1"):setVisible(true)
            img:setVisible(true)
        elseif (layer == process.level and order == process.step + 1) or 
                (layer == process.level + 1 and order == 1 and GD:getHeroInfoByIndex(process.level, process.step + 1) == nil ) then -- 将要打的
            panel:getChildByName("bg_p"):setVisible(true)
        else --不能打的
            -- donoghing
            panel:getChildByName("icon"):setOpacity(50)        
        end
    end

    -- 设置宝箱状态
    local panelIcon = self._ui.Panel_icon

    local effect = panelIcon:getChildByName("Image_32")
    local box = panelIcon:getChildByName("icon")
    local lock = panelIcon:getChildByName("icon_2")
    local open = panelIcon:getChildByName("icon_open")

    effect:setVisible(false)
    box:setVisible(false)
    lock:setVisible(false)
    open:setVisible(false)
    self:getTimeline():gotoFrameAndPause(0)

    if process:checkHeroLevelIsPass(level) then -- 已经通关
        if process:checkIsAward(level) then -- 是否已领奖
            open:setVisible(true)
        else
            effect:setVisible(true)
            box:setVisible(true)
            self:getTimeline():gotoFrameAndPlay(0)
        end
    else
        lock:setVisible(true)
    end
end

function ClassRef:showRareInfo()
    local dataTemp = {}
    dataTemp.name = L("task_road_desc2")
    dataTemp.string1 = L("task_road_desc3")

    local rarePanel = NewRareInfoView.create()
    rarePanel:setData(dataTemp)
    UIHandler:addPopupUi(rarePanel)

    -- 点击空白区域关闭
    display.nodeRemoveForNotHit(rarePanel, function()
        rarePanel:removeFromParent()
    end)

    local triggerNodeBoundingBox = self._ui["Panel_icon/icon_open"]:getBoundingBox()
    local worldPosTemp = self._ui.Panel_icon:convertToWorldSpace( cc.p(triggerNodeBoundingBox.x, triggerNodeBoundingBox.y) )
    triggerNodeBoundingBox.x = worldPosTemp.x
    triggerNodeBoundingBox.y = worldPosTemp.y
    display.adjustNodePos( triggerNodeBoundingBox, rarePanel )
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
